package com.example.weddingapp.view.dialog;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.weddingapp.R;
import com.example.weddingapp.base.binding.BaseBindingAdapter;
import com.example.weddingapp.base.binding.BaseBindingHolder;
import com.example.weddingapp.databinding.RecyclerItemDialogBottomTextBinding;
import com.google.android.material.bottomsheet.BottomSheetDialog;

import java.util.ArrayList;
import java.util.List;

import me.jingbin.library.ByRecyclerView;

/**
 * @author DaQiang
 * @date 2020/4/28 16:56
 * @desc 通用底部弹框
 */
public class BottomSheetDialogWidget implements View.OnClickListener {
    private Context mContext;
    private BottomSheetDialog bottomSheetDialog;
    private ByRecyclerView recyclerView;
    private TextView tvCancel;
    private List<Item> list;
    private MyAdapter adapter;
    private ItemClickListener itemClickListener;


    public BottomSheetDialogWidget(Context context) {
        this.mContext = context;
        this.list = new ArrayList<>();
        bottomSheetDialog = new BottomSheetDialog(mContext, R.style.BottomSheetDialog);
        View view = View.inflate(context, R.layout.dialog_bottom_sheet_staff, null);
        this.recyclerView = view.findViewById(R.id.recyclerView);
        this.tvCancel = view.findViewById(R.id.tv_cancel);
        this.tvCancel.setOnClickListener(this);
        bottomSheetDialog.setContentView(view);
    }

    public BottomSheetDialogWidget setItems(String[] items) {
        if (items != null) {
            for (String item : items) {
                Item i = new Item();
                i.setName(item);
                list.add(i);
            }
        }
        return this;
    }

    public BottomSheetDialogWidget setTextColor(int color, int position) {
        if (list != null && list.size() > 0) {
            list.get(position % list.size()).setTextColor(color);
        }
        return this;
    }

    public BottomSheetDialogWidget setOnItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
        return this;
    }

    public void show() {
        adapter = new MyAdapter();
        recyclerView.setBackgroundResource(R.drawable.dialog_bottom_selector);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(adapter);
        adapter.setNewData(list);
        recyclerView.setOnItemClickListener(new ByRecyclerView.OnItemClickListener() {
            @Override
            public void onClick(View v, int position) {
                if (itemClickListener != null) {
                    itemClickListener.onItemClick(v, position);
                }
                if (bottomSheetDialog != null && bottomSheetDialog.isShowing()) {
                    bottomSheetDialog.dismiss();
                }
            }
        });

        if (bottomSheetDialog != null && !bottomSheetDialog.isShowing()) {
            bottomSheetDialog.show();
        }
    }


    @Override
    public void onClick(View view) {
        if (bottomSheetDialog != null && bottomSheetDialog.isShowing()) {
            bottomSheetDialog.dismiss();
        }
    }

    public interface ItemClickListener {
        void onItemClick(View view, int position);
    }

    class Item {
        private String name;
        private int textColor;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getTextColor() {
            return textColor;
        }

        public void setTextColor(int textColor) {
            this.textColor = textColor;
        }
    }

    class MyAdapter extends BaseBindingAdapter<Item, RecyclerItemDialogBottomTextBinding> {

        public MyAdapter() {
            super(R.layout.recycler_item_dialog_bottom_text);
        }

        @Override
        protected void bindView(BaseBindingHolder holder, Item item, RecyclerItemDialogBottomTextBinding binding, int position) {
            binding.tvName.setText(item.getName());
            if (item.getTextColor() != 0) {
                binding.tvName.setTextColor(item.getTextColor());
            } else {
                binding.tvName.setTextColor(mContext.getResources().getColor(R.color.text_333));
            }
        }

    }
}
